/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Attributes;

import Attributes.AttributeCleaner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Alexander
 */
public class GetItunesInfo {

    private static ArrayList<String> valSet;
    private static String Track_URL;
    private static String PurchaseLink;
    private static String Path = "";
    private static String album;
    private static String genre;
    static boolean resetNoNum;

    public static ArrayList<String> getInfo(String title, String artist) {
        System.out.println("GetItunesInfo.getInfo(" + title + ", " + artist + ")");
        album = null;
        genre = null;
        resetNoNum = false;

        startConnection(title, artist);
        //System.out.println("Returning Itunes Info");
        return valSet;
    }

    public static void startConnection(String title, String artist) {
        System.out.println("GetItunesInfo.startConnection(" + title + ", " + artist + ")");
        valSet = new ArrayList();
        URL url = null;
        try {
            url = new URL("https://itunes.apple.com/search?term=" + title.replaceAll(" ", "+").replaceAll("/", "-") + "%20" + artist.replaceAll(" ", "+").replaceAll("/", "-") + "&limit=1");
            System.out.println(url.toString());
            URLConnection connection = url.openConnection();
            connection.connect();
            Path = "";

            InputStream is = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = br.readLine()) != null) {
                Path = Path + line;
            }
            JSONObject outerObject;
            JSONObject objectInArray = null;

            outerObject = new JSONObject(Path);
            JSONArray jsonArray = outerObject.getJSONArray("results");
            objectInArray = jsonArray.getJSONObject(0);

            valSet.add(getTrackURL(objectInArray));
            valSet.add(getPurchaseLink(objectInArray));
            valSet.add(getAlbum(objectInArray));
            valSet.add(getGenre(objectInArray));
            valSet.add(getTitle(objectInArray));
            valSet.add(getArtist(objectInArray));
        } catch (IOException | JSONException ex) {
            System.err.println("Error Connecting to Itunes");
            valSet.add("");
            valSet.add("");
            valSet.add("");
            valSet.add("");
            valSet.add(title);
            valSet.add(artist);
        }
    }

    public static String getTrackURL(JSONObject objectInArray) {
        String TrackURL;
        try {
            TrackURL = objectInArray.getString("previewUrl");
        } catch (JSONException ex) {
            TrackURL = "";
        }
        System.out.println("getTrackURL() - Returns - " + TrackURL);
        return TrackURL;
    }

    public static String getPurchaseLink(JSONObject objectInArray) {
        String link;
        try {
            link = objectInArray.getString("trackViewUrl");
        } catch (JSONException ex) {
            link = "";
        }
        System.out.println("getPurchaseLink() - Returns - " + link);
        return link;
    }

    public static String getAlbum(JSONObject objectInArray) {
        String album;
        try {
            album = AttributeCleaner.clean(objectInArray.getString("collectionName"));
        } catch (JSONException ex) {
            album = "";
        }
        System.out.println("getAlbum() - Returns - " + album);
        return album;
    }

    public static String getGenre(JSONObject objectInArray) {
        String genre;
        try {
            genre = AttributeCleaner.clean(objectInArray.getString("primaryGenreName"));
        } catch (JSONException ex) {
            genre = "";
        }
        System.out.println("getGenre() - Returns - " + genre);
        return genre;
    }

    public static String getTitle(JSONObject objectInArray) {
        String title;
        try {
            title = AttributeCleaner.cleanAttribute("Title", objectInArray.getString("trackName"));
        } catch (JSONException ex) {
            title = "";
        }
        System.out.println("getTitle() - Returns - " + title);
        return title;
    }

    public static String getArtist(JSONObject objectInArray) {
        String artist;
        try {
            artist = AttributeCleaner.cleanAttribute("Artist", objectInArray.getString("artistName"));
        } catch (JSONException ex) {
            artist = "";
        }
        System.out.println("getArtist() - Returns - " + artist);
        return artist;
    }
}
